//
//  UserInfoTableViewCell.swift
//  NDAi
//
//  Created by 丸子 on 2024/7/1.
//

import UIKit

class UserInfoTableViewCell: UITableViewCell {
    private let disposeBag = DisposeBag()
    var endInputClosure: ((String?) -> Void)?

    private lazy var backView = {
       let view = UIView()
        view.allRound(8)
        view.backgroundColor = UIColor(hexString: "#FFFFFF")
        return view
    }()
    
     lazy var avatarIV: UIImageView = {
       let imageView = UIImageView()
        imageView.allRound(20)
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBounds = true
        return imageView
    }()
    
     lazy var titleLb: UILabel = {
       let label = UILabel()
         label.textColor = Theme.color.text_d2
        label.font = UIFont.pingFangSCMedium(ofSize: 16)
        return label
    }()
    
    private lazy var moreIV: UIImageView = {
       let imageView = UIImageView()
        imageView.image = UIImage(named: "icon_Mine_更多")
        return imageView
    }()
    
    lazy var textField: UITextField = {
        let textField = UITextField()
        textField.borderStyle = .none
        textField.textColor = Theme.color.text_d1
        textField.textAlignment = .right
        textField.font = UIFont.pingFangSCRegular(ofSize: 14)
        textField.placeholder = "请输入"
        textField.returnKeyType = .done // 设置键盘完成按钮
        textField.delegate = self // 设置UITextFieldDelegate
        textField.attributedPlaceholder =  NSAttributedString.init(string: "", attributes: [NSAttributedString.Key.font: UIFont.pingFangSCRegular(ofSize: 14),NSAttributedString.Key.foregroundColor:Theme.color.text_d1])
        contentView.addSubview(textField)
        return textField
    }()
    
    private func bindEvent(){
         textField.rx
             .text // Get the text from the text field
             .orEmpty // Convert the optional string to a non-optional string
             .map { String($0.prefix(20)) } // Limit the string to 20 characters
             .bind(to: textField.rx.text) // Bind the limited string back to the text field
             .disposed(by: disposeBag)
     }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = UIColor.clear
        selectionStyle = .none
        contentView.addSubviews([backView])
        backView.addSubviews([avatarIV, titleLb, moreIV, textField])
        
        backView.snp.makeConstraints { make in
            make.top.equalTo(6)
            make.leading.trailing.equalToSuperview().inset(14)
            make.height.equalTo(60)
            make.bottom.equalToSuperview()
        }
        titleLb.snp.makeConstraints { make in
              make.centerY.equalToSuperview()
              make.leading.equalToSuperview().offset(18)
        }
        
        avatarIV.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(18)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(40)
        }

        moreIV.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-18)
            make.centerY.equalToSuperview()
            make.width.equalTo(9)
            make.height.equalTo(14)
        }
        
        textField.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalTo(moreIV.snp.leading).offset(-14)
            make.height.equalTo(60)
            make.leading.lessThanOrEqualTo(titleLb.snp.trailing).offset(-70)
        }
        bindEvent()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension UserInfoTableViewCell: UITextFieldDelegate {
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder() // 收起键盘
        if textField.text!.count > 0, let handler = endInputClosure {
            handler(textField.text ?? "")
        }
        return true
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        textField.resignFirstResponder() // 收起键盘
        if textField.text!.count > 0, let handler = endInputClosure {
            handler(textField.text ?? "")
        }
    }
}
